Imports System.IO
Imports Vintasoft.Pdf

Namespace Pdf
	''' <summary>
	''' Class that provides access to fonts located in specified directory
	''' and system fonts. 
	''' </summary>
	Public Class CustomFontProgramsController
		Inherits UserAndSystemFontProgramsController

		#Region "Fields"

		''' <summary>
		''' Standard font program name.
		''' </summary>
		Const DefaultStandardFontName As String = "Arial"

		''' <summary>
		''' The <see cref="PdfDocument"/>.
		''' </summary>
		Private _document As PdfDocument

		#End Region



		#Region "Constructor"

		''' <summary>
		''' Initializes a new instance of the
		''' <see cref="CustomFontProgramsController"/> class.
		''' </summary>
		Public Sub New(document As PdfDocument)
			MyBase.New("fonts\")
			_document = document
		End Sub

		#End Region



		#Region "Methods"

		''' <summary>
		''' Returns a TrueType font program use specified font name.
		''' </summary>
		''' <param name="fontName">Font name.</param>
		''' <returns>The <see cref="System.IO.Stream"/> that contains the font
		''' program in TrueType format if font is found successfully;
		''' otherwise, <b>null</b>.</returns>
		Public Overrides Function GetTrueTypeFontProgram(fontName As String) As Stream
			Dim result As Stream = MyBase.GetTrueTypeFontProgram(fontName)
			' if font program is not found then
			If result Is Nothing Then
				' use standard font
				Dim standardFontName As String = DefaultStandardFontName
				If fontName.StartsWith("Courier") Then
					standardFontName = "Courier New"
				End If
				Dim fontNameUpper As String = fontName.ToUpper()
				If fontNameUpper.EndsWith("BOLD ITALIC") Then
					standardFontName += " Bold Italic"
				ElseIf fontNameUpper.EndsWith("BOLD") Then
					standardFontName += " Bold"
				ElseIf fontNameUpper.EndsWith("ITALIC") Then
					standardFontName += " Italic"
				End If
				result = MyBase.GetTrueTypeFontProgram(standardFontName)
				If result IsNot Nothing Then
					Dim message As String = String.Format("Font program '{0}' was not found, '{1}' program is used.", fontName, standardFontName)
					_document.RuntimeMessages.Add(New PdfRuntimeWarning("FontProgramsController", message))
				End If
			End If
			Return result
		End Function

		#End Region

	End Class
End Namespace
